package com.trailwalker.traceability.widget.dialog;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class JfxLoadingForm {

    private Stage primaryStage = null;
    private Stage loadingStage = null;
    private final Label lblLoading;
    private final VBox vbLoading;
    private final ProgressIndicator piLoading;

    public JfxLoadingForm(Stage primaryStage) {
        this.primaryStage = primaryStage;
        loadingStage = new Stage();

        //窗口设置
        loadingStage.initOwner(primaryStage);
        loadingStage.initStyle(StageStyle.TRANSPARENT);
        loadingStage.initModality(Modality.APPLICATION_MODAL);

        //进度条设置
        lblLoading = new Label("加载中");
        lblLoading.setFont(Font.font(20));
        lblLoading.setTextFill(Color.WHITE);
        piLoading = new ProgressIndicator(-1);
        piLoading.setStyle("-fx-accent:'#ffffff';");

        vbLoading = new VBox();
        vbLoading.setSpacing(10);
        vbLoading.setBackground(new Background(new BackgroundFill(Paint.valueOf("#000000bb"), CornerRadii.EMPTY, Insets.EMPTY)));
        vbLoading.getChildren().addAll(piLoading, lblLoading);
        vbLoading.setAlignment(Pos.CENTER);

        Scene scene = new Scene(vbLoading);
        scene.setFill(null);
        loadingStage.setScene(scene);
        loadingStage.setHeight(primaryStage.getHeight());
        loadingStage.setWidth(primaryStage.getWidth());
        loadingStage.setX(primaryStage.getX());
        loadingStage.setY(primaryStage.getY());
    }

    public JfxLoadingForm setSpacing(double value) {
        vbLoading.setSpacing(value);
        return this;
    }

    public JfxLoadingForm setText(String value) {
        lblLoading.setText(value);
        return this;
    }

    public JfxLoadingForm setFont(Font value) {
        lblLoading.setFont(value);
        return this;
    }

    public JfxLoadingForm setTextFill(Paint value) {
        lblLoading.setTextFill(value);
        return this;
    }

    public JfxLoadingForm setProgressIndicatorBackground(String value) {
        String background = "-fx-accent:'" + value + "';";
        piLoading.setStyle(background);
        return this;
    }

    public JfxLoadingForm setProgressIndicatorSize(double value) {
        piLoading.setPrefHeight(value);
        piLoading.setPrefWidth(value);
        return this;
    }

    public void show() {
        loadingStage.show();
    }

    public void close() {
        loadingStage.close();
    }
}
